/**
 * 表情宅App路由
 */
const axios = require('axios')
const express = require('express')
const bqzRouter = express.Router();
const fs = require('fs')
const path = require('path')
const tokenPath = path.resolve(__dirname, '../token.txt')

const baseParams = {
    _b: 'com.bq1000.teddy',
    _d: '0',
    _l: '',
    _o: 'android',
    _s: '10',
    _u: '2c7640e0254a8cab',
    _v: '2.0',
    start: '',
    limit: 30,
    keyword: '早上好'
}

const baseHeaders = {
    host: 'dou.imd.im',
    userid: 'WdAhSFo5V',
    authorization: 'Bearer ' + fs.readFileSync(tokenPath),
    userAgent: 'Mozilla/5.0 (Linux; Android 10; Mi 10 Build/QKQ1.191117.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/80.0.3987.99 Mobile Safari/537.36'
}

// 动态表情
bqzRouter.get('/trends', async (req, res) => {
    try {
        const result = await axios.get('https://dou.imd.im/api/status/list/by_user', {
            params: Object.assign({}, baseParams, {
                start: req.query.start ? req.query.start : baseParams.start,
                limit: req.query.limit ? req.query.limit : baseParams.limit
            }),
            headers: baseHeaders
        })
        res.send(result.data)
    } catch (e) {
        res.send('error')
    }
})

// 收藏表情
bqzRouter.get('/collect', async (req, res) => {
    try {
        const result = await axios.get('https://dou.imd.im/api/emotion/list/by_category', {
            params: Object.assign({}, baseParams, {
                categoryid: 'pOgGM-tvM',
                start: req.query.start ? req.query.start : baseParams.start,
                limit: req.query.limit ? req.query.limit : baseParams.limit
            }),
            headers: baseHeaders
        })
        res.send(result.data)
    } catch (e) {
        console.log(e)
    }
})

// 热门表情
bqzRouter.get('/hot', async (req, res) => {
    try {
        const result = await axios.get('https://dou.imd.im/api/emotion/list/by_category', {
            params: Object.assign({}, baseParams, {
                categoryid: 'hot',
                start: req.query.start ? req.query.start : baseParams.start,
                limit: req.query.limit ? req.query.limit : baseParams.limit
            }),
            headers: baseHeaders
        })
        res.send(result.data)
    } catch (e) {
        console.log(e)
    }
})

// 最新表情
bqzRouter.get('/new', async (req, res) => {
    try {
        const result = await axios.get('https://dou.imd.im/api/emotion/list/by_category', {
            params: Object.assign({}, baseParams, {
                categoryid: 'new',
                start: req.query.start ? req.query.start : baseParams.start,
                limit: req.query.limit ? req.query.limit : baseParams.limit
            }),
            headers: baseHeaders
        })
        res.send(result.data)
    } catch (e) {
        console.log(e)
    }
})

// 搜索表情
bqzRouter.get('/search', async (req, res) => {
    try {
        const result = await axios.get('https://dou.imd.im/api/emotion/list/by_search', {
            params: Object.assign({}, baseParams, {
                keyword: req.query.keyword ? req.query.keyword : baseParams.keyword,
                start: req.query.start ? req.query.start : baseParams.start,
                limit: req.query.limit ? req.query.limit : baseParams.limit
            }),
            headers: baseHeaders
        })
        res.send(result.data)
    } catch (e) {
        console.log(e)
    }
})

// 搜索表情
bqzRouter.get('/login', async (req, res) => {
    try {
        const result = await axios.post('https://dou.imd.im/api/login', {
            username: '13129304772',
            password: 'zxc12300'
        }, {
            params: {
                _v: '2.0',
                _o: 'android',
                _s: 11,
                _b: 'com.bq1000.teddy',
                _l: '',
                _u: '3fe57903072a9e36',
                _d: 0
            },
            headers: {
                host: 'dou.imd.im',
                userAgent: 'Mozilla/5.0 (Linux; Android 11; Mi 10 Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/90.0.4430.210 Mobile Safari/537.36'
            }
        })
        if (result.data.data.token) {
            fs.writeFileSync(tokenPath, result.data.data.token)
        }
        res.send(result.data.data)
    } catch (e) {
        console.log(e)
    }
})


module.exports = bqzRouter